package org.sith.gl.taskmanager.server.db.service.impl;

import org.sith.gl.taskmanager.server.db.service.DefaultDAOService;
import org.springframework.orm.jpa.support.JpaDaoSupport;

import java.lang.reflect.ParameterizedType;
import java.util.List;


public abstract class DefaultDAOServiceImpl<T> extends JpaDaoSupport implements DefaultDAOService<T> {
    private Class<T> persistentClass;

    protected DefaultDAOServiceImpl() {
        this.persistentClass = (Class<T>) ((ParameterizedType) getClass()
				.getGenericSuperclass()).getActualTypeArguments()[0];
    }

    public T saveOrUpdate(T t) {
        return getJpaTemplate().merge(t);
    }

    public T findById(Long id) {
        return getJpaTemplate().find(persistentClass,id);
    }

    public List<T> findAll() {
        return getJpaTemplate().find("from " + persistentClass.getName());
    }

    public void remove(T t) {
        getJpaTemplate().remove(t);
    }
}
